package cn.cau.control;

import cn.cau.entity.BasicEnumSum;
import cn.cau.entity.CodeNumber;
import cn.cau.entity.CodeRule;
import cn.cau.entity.NonEnum;
import cn.cau.service.BasicEnumService;
import cn.cau.service.CodeNumberService;
import cn.cau.service.CodeRuleService;
import cn.cau.service.NonEnumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import java.io.PrintWriter;
import java.util.List;

@Controller
public class CodeSearchController {

    @Autowired
    private CodeNumberService codeSearchService;

    @Autowired
    private CodeRuleService codeRuleService;

    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;

    @RequestMapping(value="/inputKey",method=RequestMethod.POST)
    public ModelAndView inputKey(@RequestParam(value = "code_number")String code_number)
    {
        String errorString="";
        List<CodeNumber> c1=codeSearchService.getAllRecoreds(code_number);
        if(c1.size()==0)
        {
            errorString = "查询结果为空，请重新输入查询标识";
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("error", errorString);
            modelAndView.setViewName("/codeSearch/codeSearch");
            return modelAndView;
        }
        else {
            ModelAndView modelAndView = new ModelAndView();
            modelAndView.addObject("code_number_result", code_number);
            modelAndView.addObject("codenumberresult", c1);
            modelAndView.addObject("error", errorString);
            modelAndView.setViewName("/codeSearch/codeSearch");
            return modelAndView;
        }
    }

    @RequestMapping(value="/codeSearchApply",method=RequestMethod.GET)
    public ModelAndView CodeSearchApply(@RequestParam(value = "id") int id){
        ModelAndView modelAndView = new ModelAndView();
        List<CodeNumber> c2=codeSearchService.getAllRecored(id);
        CodeRule rule=codeRuleService.findCodeRuleById(c2.get(0).getCodeRuleId());
        int coderule=c2.get(0).getCodeRuleId();
        String codenumbe=c2.get(0).getCodeNumber();
        String rulecategoryname=codeSearchService.getRuleCategoryName(c2.get(0).getRuleCategoryId());
        String categoryname=codeSearchService.getCategoryName(c2.get(0).getCategoryId());
        String rulee=rule.toString();
        String coderulename=rule.getCodeRuleName();
        List<BasicEnumSum> enumsum=basicEnumService.findBasicEnumSumByCodeRuleId(rule.getCodeRuleId());
        List<NonEnum> nonenumsum=nonEnumService.findNonEnumByCodeId(rule.getCodeRuleId());
        String codeintro="该编号表示：";
        String temp1="";
        String temp2="";
        String codenumb_temp = codenumbe.substring(3);
        int codecount=rule.getCodeCodeCount();
        int k=0;
        int j=0;
        int len=0;
        for (int i = 1; i <= codecount; i++) {
            if(enumsum.get(k).getCodeCodeId()==i)
            {
                len=enumsum.get(k).getCodeLength();
                temp1=enumsum.get(k).getDetails();
                temp2=codenumb_temp.substring(0,len);
                k++;
                codeintro=codeintro+temp1+"为"+temp2;
                codenumb_temp=codenumb_temp.substring(len);
            }
            else
            {
                len=nonenumsum.get(j).getCodeLength();
                temp1=nonenumsum.get(j).getDetails();
                temp2=codenumb_temp.substring(0,len);
                j++;
                codeintro=codeintro+temp1+"为"+temp2;
                codenumb_temp=codenumb_temp.substring(len);
            }
            if(i!=codecount)
            {
                codeintro+="、";
            }
        }
        codeintro+="。";
        modelAndView.addObject("number",codenumbe);
        modelAndView.addObject("rule",codeintro);
        modelAndView.addObject("coderuleid",coderule);
        modelAndView.addObject("coderule",coderulename);
        modelAndView.addObject("rulecategoryname",rulecategoryname);
        modelAndView.addObject("categoryname",categoryname);
        modelAndView.setViewName("/codeSearch/codeSearchResult");

        return modelAndView;
    }




}
